<?php
namespace Blind\Controller;
use Think\Controller;
class IndexController extends Controller {


    public function dologin()
    {
        $data = I('post.');
        $name = "'".$data['name']."'";
        $password = md5($data['password']);
        $res = M('admin') -> where("name = $name") -> select();

        if($res && $res[0]['password'] == $password)
        {
            session('user', $res);
            $this -> success('成功', 'index');
        }else
        {
            $this -> error('失败');
        }
    }

    public function outlogin()
    {
        session('user', false);
        $this -> display('login');
    }
    //跳转活动类页面
    public function index()
    {
        if(session('user') == true)
        {
            $res = M('activity') -> select();

            $this -> assign('data', $res);

            $this -> display();
        }else
        {
            $this -> display('login');
        }

    }

    public function add()
    {
        $this -> display('add');
    }
    //添加
    public function huodongAdd()
    {
        $data['name'] = I('post.name');
        $y = I('post.year');
        $m = I('post.month');
        $d = I('post.day');
        $data['endtime'] = $y."-".$m."-".$d;
        $data['introduce'] = I('post.introduce');

        $res = M('activity') -> add($data);
        if($res)
        {
            $this -> success('成功', 'index');
        }else
        {
            $this -> error('失败');
        }
    }
    //删除
    public function activityDel($id)
    {
        $res = M('activity') -> where("id=%d",$id) -> delete();
        if($res)
        {
            $this -> success('成功');
        }else
        {
            $this -> error('失败');
        }
    }

        //编辑广告
    public function activityUpd()
    {
        $data['name'] = I('post.name');
        $y = I('post.year');
        $m = I('post.month');
        $d = I('post.day');
        $data['endtime'] = $y."-".$m."-".$d;
        $data['introduce'] = I('post.introduce');

        $id = I('post.id');
        $res = M('activity') -> where("id=%d",$id)-> save($data);
        if($res)
        {
            $this -> success('成功', 'index');
        }else
        {
            $this -> error('失败');
        }
    }

    //编辑时获取单个信息
    //
    public function activityFind($id)
    {

        $res = M('activity') -> where("id=%d",$id) -> select();

        $time = explode('-',$res[0]['endtime']);
        $year = $time[0];
        $month = $time[1];
        $day = $time[2];
        $res[0]['year'] = $year;
        $res[0]['month'] = $month;
        $res[0]['day'] = $day;
        if($res)
        {
            $this -> assign('data', $res);
            $this -> display();
        }else
        {
            $this -> error('编辑失败');
        }

    }

}